<?php
namespace Home\Controller;

use Common\Model\RecordModel;
use Think\Controller;

class ApiController extends Controller
{

    /**
     * 返回错误信息
     * @param $info
     * @return string
     */
    public function wrong($info)
    {
        die(json_encode(array('info'=>$info,'status'=>0)));
    }

    /**
     * 返回正确信息
     * @author Mr.Alien
     * @param $info
     * @return string
     */
    public function right($info)
    {
        die(json_encode(array('info'=>$info,'status'=>1)));
    }

    /**
     * 获取用户的快件
     * @author Mr.Alien
     * @return string
     */
    public function getList()
    {
        $phone = I('get.phone');
        $preg = '/^1[3578]\d{9}$/';
        if(!preg_match($preg,$phone)){
            $this->wrong('参数有误');
        }
		$where['phone'] = $phone;	
		$where['status'] = 1;
		if($_GET['stock'] == 1 || $_GET['stock'] === '0' ){
			$where['stock'] = $_GET['stock'];
		}
		if(I('get.name')){
			$where['name'] = array('like','%'.I('get.name').'%');
		}
        $list = D('Take')->relation(true)->where($where)->select();
        $new_list = array();
        foreach($list as $key => $value){
            $new_list[$key]['id'] = $value['id'];
            $new_list[$key]['no'] = $value['no'];
			 $new_list[$key]['name'] = $value['name'];
            $new_list[$key]['phone'] = $value['phone'];
            $new_list[$key]['addr'] = $value['addr'];
            $new_list[$key]['roleid'] = $value['roleid'];
            $new_list[$key]['container_no'] = $value['container']['no'];
            $new_list[$key]['create_time'] = $value['create_time'];
			$new_list[$key]['rr_state'] = $value['rr_state'];
			$new_list[$key]['stock'] = $value['stock'];
			$new_list[$key]['since'] = $value['since'];
        }
        $this->right($new_list);
    }

    /**
     *上传客户信息
     * @author Mr.Alien
     * @return string
     */
    public function uploadCustomer()
    {
        $data = I('post.');
        $preg = '/^1[3578]\d{9}$/';
        !preg_match($preg,$data['phone']) && $this->wrong('手机号码有误！');
        $has = M('Customer')->where(array('phone'=>$data['phone']))->find();
        $has && $this->wrong('已存在此用户信息！');
        !$data['name'] && $this->wrong('用户姓名不可为空');
        !$data['card'] && $this->wrong('身份证号不可为空');
        !$data['card_image'] && $this->wrong('身份证照片不可为空');
        $res = M('Customer')->add($data);
        if($res === false){
            $this->wrong('保存失败');
        }else{
            $this->right('保存成功');
        }
    }

    /**
     * 获取寄件价格
     * @author Mr.Alien
     * @return int
     */
    public function getPrice()
    {
        $data = I('post.');
        !$data['roleid'] && $this->wrong('网点ID不可为空');
        !$data['province_id'] && $this->wrong('目的地不可为空');
        !$data['weight'] && $this->wrong('输入重量');
        $user = M('User')->where(array('roleid'=>$data['roleid']))->find();
        if($customer = M('Customer')->where(array('user_id'=>$user['id'],'phone'=>$data['phone']))->find()){
            $info = M('Price')->where(array('user_id'=>$user['id'],'province_id'=>$data['province_id'],'template_name'=>$customer['template_name']))->limit(1)->getField('price');
        }else{
            $info = M('Price')->where(array('user_id'=>$user['id'],'province_id'=>$data['province_id']))->order("price desc")->limit(1)->getField('price');
        }
        !$info && $this->wrong('网点未设置价格');
        if($data['weight'] < $info['range']){
            $this->right($info['price']);
        }else{
            $this->right($info['price']+($data['weight']-$info['range'])*$info['excess_price']);
        }
    }

    /**
     * 修改订单状态
     * @author Mr.Alien
     * @return string
     */
    public function setStatus()
    {
        $data = I('post.');
        $res = M('Take')->save($data);
        if($res === false){
            $this->wrong('保存失败');
        }else{
            $this->right('保存成功');
        }
    }

    /**
     * 上传寄件信息
     * @author Mr.Alien
     * @return string
     */
    public function uploadDeliver()
    {
        $data = I('post.');
        $user = M('User')->where(array('roleid'=>$data['roleid']))->find();	
        !$user && $this->wrong('无网点信息');
        $data['user_id'] = $user['id'];
        $data['status'] = 0;
		$data['is_ren'] = 2;
        $data['no'] = RecordModel::getAvailableNo($user['id']);
        !$data['no'] && $this->error('无可用订单号');
        $res = M('Post')->add($data);
        if($res === false){
            $this->wrong('保存失败');
        }else{
            die(json_encode(array('info'=>$res,'status'=>1,'no'=>$data['no'])));
			
        }
    }
	
	/**
	 * 修改寄件状态	 
	 **/
	 public function setDeliver()
    {
        $data = I('post.');
        $res = M('Post')->save($data);
        if($res === false){
            $this->wrong('保存失败');
        }else{
            $this->right('保存成功');
        }
    }
	
	/**
	*查询可用订单
	**/
	public function getNo($roleid)
	{
		$user = M('User')->where(array('roleid'=>$roleid))->find();
		$info = M("Record")->where(array('status'=>1,'user_id'=>$user['id']))->order("no asc")->find();
		if($user && $info){		
            $this->right('有可用单号');
        }else{
            $this->wrong('无可用单号');
        }
	}

    /**
    *   获取所有网点列表
    **/
    public function getNest()
    {
        $list = M('User')->select();
        $nest_list = array();
        foreach ($list as $key => $value) {
            if($value['roleid'] && ($value['roleid'] != 1)){
                $nest_list[] = array('roleid'=>$value['roleid'],'name'=>$value['express_name']);
            }
        }
        $this->right($nest_list);
    }
	 
	


}